package com.by.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;

@WebServlet("/upServlet")
@MultipartConfig
public class UpServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        //获得上传文件
        Part file = req.getPart("myfile");
        //文件信息
        System.out.println("文件名：" + file.getSubmittedFileName());
        System.out.println(file.getContentType());
        System.out.println(file.getSize());

        file.write(file.getSubmittedFileName());

        resp.getWriter().write("上传成功" + file.getSubmittedFileName());

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }


}
